package weaver.interfaces.linearin.duss.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-30
 * @Description: 传金蝶凭证和应付单
 * @ModifyLog:
 **/

public class KingdeeUtil {
    /*
     *@Description: 其他业务应付单
     *@Param: [paramJson]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2023/12/06
     */
    public static JSONObject otherBusinessPay(JSONObject paramJson,Map<String, Object> dataMap) {
        JSONObject params = new JSONObject();
        JSONObject Model = new JSONObject();
        Model.put( "FID", "0" );
        //单据类型-默认为其他业务付款单
        JSONObject FBillTypeID = new JSONObject();
        FBillTypeID.put( "FNUMBER", "FKDLX02_SYS");
        Model.put( "FBillTypeID", FBillTypeID );
        //业务日期=支付日期
        Model.put( "FDATE", paramJson.get( "rq" ) + " 00:00:00" );
        //往来单位类型-默认为供应商
        Model.put( "FCONTACTUNITTYPE", "BD_Supplier" );
        //往来单位=收款单位
        JSONObject FCONTACTUNIT = new JSONObject();
        FCONTACTUNIT.put( "FNumber", paramJson.get( "jdrybm" ) );
        Model.put( "FCONTACTUNIT", FCONTACTUNIT );
        //收款单位类型-默认为供应商
        Model.put( "FRECTUNITTYPE", "BD_Supplier" );
        //收款单位
        JSONObject FRECTUNIT = new JSONObject();
        FRECTUNIT.put( "FNumber", paramJson.get( "jdrybm" ) );
        Model.put( "FRECTUNIT", FRECTUNIT );
        //币别
        JSONObject FCURRENCYID = new JSONObject();
        FCURRENCYID.put( "FNumber", paramJson.get( "bz" ) );
        Model.put( "FCURRENCYID", FCURRENCYID );
        //付款组织=默认为上海先积集成电路有限公司
        JSONObject FPAYORGID = new JSONObject();
        FPAYORGID.put( "FNumber", paramJson.get( "zz" ));
        Model.put( "FPAYORGID", FPAYORGID );
        //结算组织=默认为上海先积集成电路有限公司
        JSONObject FSETTLEORGID = new JSONObject();
        FSETTLEORGID.put( "FNumber", paramJson.get( "zz" ) );
        Model.put( "FSETTLEORGID", FSETTLEORGID );

        //付款单明细：FPAYBILLENTRY
        JSONArray FPAYBILLENTRY = new JSONArray();
        JSONObject FPAYBILLENTRYJSON = new JSONObject();
        //结算方式=结算方式
        JSONObject FSETTLETYPEID = new JSONObject();
        FSETTLETYPEID.put( "FNumber", "09" );
        FPAYBILLENTRYJSON.put( "FSETTLETYPEID", FSETTLETYPEID );
        //付款用途=付款用途
        JSONObject FPURPOSEID = new JSONObject();
        FPURPOSEID.put( "FNumber", "SFKYT12_SYS" );
        FPAYBILLENTRYJSON.put( "FPURPOSEID", FPURPOSEID );
        //应付金额=借款金额
        FPAYBILLENTRYJSON.put( "FPAYTOTALAMOUNTFOR", paramJson.get( "je" ) );
        //我方银行账号=支付账户
        JSONObject FACCOUNTID = new JSONObject();
        FACCOUNTID.put( "FNumber", paramJson.get( "zfzh" ) );
        FPAYBILLENTRYJSON.put( "FACCOUNTID", FACCOUNTID );
        FPAYBILLENTRY.add( FPAYBILLENTRYJSON );
        Model.put( "FPAYBILLENTRY", FPAYBILLENTRY );
        params.put( "Model", Model );
        return InvokeHelper.Save("AP_PAYBILL", params.toJSONString(),dataMap );
    }

    /*
     *@Description: 采购业务应付单
     *@Param: [paramJson]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2023/12/06
     */
    public static JSONObject procurementBusinessPay(JSONObject paramJson,Map<String, Object> dataMap) {
        JSONObject params = new JSONObject();
        JSONObject Model = new JSONObject();
        Model.put( "FID", "0" );
        //单据类型-默认为采购业务付款单
        JSONObject FBillTypeID = new JSONObject();
        FBillTypeID.put( "FNUMBER", "FKDLX01_SYS" );
        Model.put( "FBillTypeID", FBillTypeID );
        //业务日期=付款日期
        Model.put( "FDATE", paramJson.get( "rq" ) + " 00:00:00" );
        //往来单位类型-默认供应商
        Model.put( "FCONTACTUNITTYPE", "BD_Supplier" );
        //往来单位-收款单位
        JSONObject FCONTACTUNIT = new JSONObject();
        FCONTACTUNIT.put( "FNumber", paramJson.get( "jdrybm" ) );
        Model.put( "FCONTACTUNIT", FCONTACTUNIT );
        //收款单位类型-默认供应商
        Model.put( "FRECTUNITTYPE", "BD_Supplier" );
        //收款单位=收款单位
        JSONObject FRECTUNIT = new JSONObject();
        FRECTUNIT.put( "FNumber", paramJson.get( "jdrybm" ) );
        Model.put( "FRECTUNIT", FRECTUNIT );

        Model.put( "FISINIT", false );

        //币别
        JSONObject FCURRENCYID = new JSONObject();
        FCURRENCYID.put( "FNumber", paramJson.get( "bz" ) );
        Model.put( "FCURRENCYID", FCURRENCYID );

        Model.put( "FEXCHANGERATE", 1.0 );
        Model.put( "FSETTLERATE", 1.0 );


        //结算组织-默认为上海先积集成电路有限公司
        JSONObject FSETTLEORGID = new JSONObject();
        FSETTLEORGID.put( "FNumber", paramJson.get( "zz" ) );
        Model.put( "FSETTLEORGID", FSETTLEORGID );

        //采购组织-默认为上海先积集成电路有限公司
        JSONObject FPURCHASEORGID = new JSONObject();
        FPURCHASEORGID.put( "FNumber",paramJson.get( "zz" ) );
        Model.put( "FPURCHASEORGID", FPURCHASEORGID );

        Model.put( "FDOCUMENTSTATUS", "Z" );
        Model.put( "FBUSINESSTYPE", "2" );
        Model.put( "FCancelStatus", "A" );

        //付款组织-默认为上海先积集成电路有限公司
        JSONObject FPAYORGID = new JSONObject();
        FPAYORGID.put( "FNumber", "1003" );
        Model.put( "FPAYORGID", FPAYORGID );

        Model.put( "FISSAMEORG", true );
        Model.put( "FIsCredit", false );

        JSONObject FSETTLECUR = new JSONObject();
        FSETTLECUR.put( "FNUMBER", "PRE001" );
        Model.put( "FSETTLECUR", FSETTLECUR );

        Model.put( "FIsWriteOff", false );
        Model.put( "FREALPAY", false );
        Model.put( "FISCARRYRATE", false );

        JSONObject FSETTLEMAINBOOKID = new JSONObject();
        FSETTLEMAINBOOKID.put( "FNUMBER", "PRE001" );
        Model.put( "FSETTLEMAINBOOKID", FSETTLEMAINBOOKID );

        Model.put( "FMoreReceive", false );

        //付款单明细：FPAYBILLENTRY
        JSONArray FPAYBILLENTRY = new JSONArray();
        JSONObject FPAYBILLENTRYJSON = new JSONObject();
        //结算方式-默认为银行转账
        JSONObject FSETTLETYPEID = new JSONObject();
        FSETTLETYPEID.put( "FNumber", "09" );
        FPAYBILLENTRYJSON.put( "FSETTLETYPEID", FSETTLETYPEID );
        //付款用途-默认采购付款
        JSONObject FPURPOSEID = new JSONObject();
        FPURPOSEID.put( "FNumber", "SFKYT08_SYS" );
        FPAYBILLENTRYJSON.put( "FPURPOSEID", FPURPOSEID );
        //应付金额=应付金额
        FPAYBILLENTRYJSON.put( "FPAYTOTALAMOUNTFOR", paramJson.get( "je" ) );
        FPAYBILLENTRYJSON.put( "FPAYAMOUNTFOR_E", paramJson.get( "je" ) );
        FPAYBILLENTRYJSON.put( "FSETTLEPAYAMOUNTFOR", paramJson.get( "je" ) );
        FPAYBILLENTRYJSON.put( "FREALPAYAMOUNTFOR_D", paramJson.get( "je" ) );
        //我方银行账号=支付账户
        JSONObject FACCOUNTID = new JSONObject();
        FACCOUNTID.put( "FNumber", paramJson.get( "zfzh" ) );
        FPAYBILLENTRYJSON.put( "FACCOUNTID", FACCOUNTID );

        FPAYBILLENTRYJSON.put( "FPAYAMOUNT_E", paramJson.get( "je" ) );
        FPAYBILLENTRYJSON.put( "FPOSTDATE", paramJson.get( "rq" ) + " 00:00:00" );
        FPAYBILLENTRYJSON.put( "FRuZhangType", "1" );
        FPAYBILLENTRYJSON.put( "FPayType", "A" );

        FPAYBILLENTRY.add( FPAYBILLENTRYJSON );
        Model.put( "FPAYBILLENTRY", FPAYBILLENTRY );
        params.put( "Model", Model );
        return InvokeHelper.Save( "AP_PAYBILL", params.toJSONString(),dataMap);
    }

    /*
     *@Description: 传凭证
     *@Param: [paramJson]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2023/11/30
     */
    public static JSONObject passCertificate(JSONObject paramJson, Map<String, Object> dataMap) {
        JSONObject params = new JSONObject();
        JSONObject Model = new JSONObject();
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog(">>>>>passCertificate>>>>>paramJson="+paramJson);
        //实体主键
        Model.put( "FVOUCHERID", 0 );
        //账簿
        JSONObject FAccountBookID = new JSONObject();
        FAccountBookID.put( "FNumber", Util.null2String(dataMap.get("bookid")) );
        Model.put( "FAccountBookID", FAccountBookID );
        //附件数
        Model.put( "FATTACHMENTS", 2 );
        //日期
        Model.put( "FDate", paramJson.getString( "zfrq" ) + " 00:00:00" );
        //业务日期
        Model.put( "FBUSDATE", paramJson.getString( "zfrq" ) + " 00:00:00" );
        //凭证字
        JSONObject FVOUCHERGROUPID = new JSONObject();
        FVOUCHERGROUPID.put( "FNumber", "PRE001" );
        Model.put( "FVOUCHERGROUPID", FVOUCHERGROUPID );
        //凭证号
        Model.put( "FVOUCHERGROUPNO", "68" );
        //是否调整期凭证
        Model.put( "FISADJUSTVOUCHER", false );
        //审核状态
        Model.put( "FDocumentStatus", "Z" );
        //会计年度
        Model.put( "FYEAR", paramJson.getString( "zfrq" ).substring( 0, 4 ) );
        //业务类型
        JSONObject FSourceBillKey = new JSONObject();
        FSourceBillKey.put( "FNumber", "78050206-2fa6-40e3-b7c8-bd608146fa38" );
        Model.put( "FSourceBillKey", FSourceBillKey );
        //期间
        Model.put( "FPERIOD", paramJson.getString( "zfrq" ).substring( 5, 7 ) );
        JSONArray FEntity = JSONArray.parseArray(paramJson.getString("FEntity" ));
        Model.put( "FEntity", FEntity);
        params.put( "Model", Model );
        baseBean.writeLog(">>>>>passCertificate>>>>>params="+params);
        return InvokeHelper.Draft( "GL_VOUCHER", params.toJSONString(),dataMap );
    }

    /**
     * 获取预算科目名称
     * @param id
     * @return
     */
    public static String getYskmmc(String id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select name from fnabudgetfeetype where id=?",id);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     * 获取预算科目名称
     * @param id
     * @return
     */
    public static String getYfdlBm(String id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select bm from uf_xjyfdl where id=?",id);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     * 获取账簿ID
     * @param departid
     * @return
     */
    public static String getBookId(String departid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select ztid from uf_xjzt where syb=?",departid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     * 获取组织ID
     * @param departid
     * @return
     */
    public static String getOrganId(String departid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select bm from uf_xjzz where syb=?",departid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     * 获取项目编码
     * @param id
     * @return
     */
    public static String getXmbh(String id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select xmbh from uf_xjxmtz where id=?",id);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     * 获取供应商名称
     * @param id
     * @return
     */
    public static String getGysmc(String id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select gysmc from uf_gysb where id=?",id);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     * 获取供应商编码
     * @param id
     * @return
     */
    public static String getGysBm(String id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select gysbm from uf_gysb where id=?",id);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

}
